package com.yunxin.core.customer.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.core.admin.domain.AdminUser;
import com.yunxin.core.admin.service.AdminUserService;
import com.yunxin.core.customer.domain.CustomerTelephonerLog;
import com.yunxin.core.customer.service.CustomerTelephonerLogService;
import com.yunxin.core.customer.mapper.CustomerTelephonerLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【customer_telephoner_log(客户信息分配话务员日志表)】的数据库操作Service实现
* @createDate 2024-08-14 17:02:17
*/
@DS("crm")
@Service
public class CustomerTelephonerLogServiceImpl extends ServiceImpl<CustomerTelephonerLogMapper, CustomerTelephonerLog>
    implements CustomerTelephonerLogService{

    @Autowired
    private AdminUserService adminUserService;

    @Override
    public List<CustomerTelephonerLog> listByCustomerId(Long customerId) {
        LambdaQueryWrapper<CustomerTelephonerLog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerTelephonerLog::getCustomerInfoId,customerId)
                .orderByDesc(CustomerTelephonerLog::getCreateTime);
        List<CustomerTelephonerLog> list = this.list(queryWrapper);
        if (!list.isEmpty()){
            Set<Long> userIds = list.stream().map(i -> i.getUserId()).collect(Collectors.toSet());
            List<AdminUser> adminUsers = adminUserService.getAdminUserListByIds(userIds);
            Map<Long, String> adminUserMap = adminUsers.stream().collect(Collectors.toMap(i -> i.getId().longValue(), obj -> obj.getUsername()));
            for (CustomerTelephonerLog customerTelephonerLog : list) {
                customerTelephonerLog.setUserName(adminUserMap.get(customerTelephonerLog.getUserId()));
            }
        }
        return list;
    }

    @Override
    public Integer getAllotCount(Long customerId,Integer type) {
        LambdaQueryWrapper<CustomerTelephonerLog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerTelephonerLog::getCustomerInfoId,customerId)
                .eq(CustomerTelephonerLog::getType,type);
        long count = this.count(queryWrapper);
        return (int)count;
    }
}




